﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.BusinessLayer.BLL;
using JKF.Entities;
using System.Data;
namespace JKF.HandlerLayer.Product
{
    public partial class Stock_AmountHandler:AHttpHandler
    {
        private bllSTOCK_AMOUNT bllStock_Amount = new bllSTOCK_AMOUNT();
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }
        /// <summary>
        /// 读取库存
        /// </summary>
        public void GetStock_Amounts()
        {
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;
            //从视图中获取
            obj.Entity = from a in bllStock_Amount.GetStock_AmountsBLL(bllSYS_USER.CurrentUser.SHOP_CODE, P("PRODUCT_KEY"), P("MODEL"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {//PRODUCT_KEY,   MODEL, TYPE, IN_NO, DATE_IN, MEMO, SORT_NAME, SHOP_CODE, PRODUCT_NUMS, USER_CREATE, 
                             PRODUCT_KEY = a.Field<Guid>("PRODUCT_KEY"),
                             SHOP_CODE = a.Field<string>("SHOP_CODE"),  
                             MODEL = a.Field<string>("MODEL"),  
                             NAME = a.Field<string>("NAME"),
                             SORT_NAME = a.Field<string>("SORT_NAME"),
                             PRODUCT_NUMS = a.Field<int>("PRODUCT_NUMS") 
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        /// <summary>
        /// 判断出库，给予相应提示
        /// </summary>
        public void IsProductStock_Out()
        {
            try
            {
                var product_key = P("PRODUCT_KEY");
                var product_nums = P("PRODUCT_NUMS");
                var stock_amount_model = bllStock_Amount.GetModel(new Guid(product_key), bllSYS_USER.CurrentUser.SHOP_CODE);
                //如果出库数量大于库存时或者库存为零时，给予提示
                if (stock_amount_model == null || Convert.ToInt32(product_nums) > stock_amount_model.PRODUCT_NUMS)
                {
                    Response.Write(new ResponseInf
                    {
                        Data = "库存不足，是否继续出库？",
                        Success = true
                    }.ToJson());
                    return;
                }
                throw new Exception("库存够");
            }
            catch (Exception exp)
            {
                Response.Write(new ResponseInf { Success = false, Message = exp.Message }.ToJson());
            }
            
        }
    }
}
